package org.jpropelleralt.utils.impl;

import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Utils for working with {@link Logger}
 */
public class LoggerUtils {

	/**
	 * Enable console logging.
	 * Enable {@link Level#FINEST} logging for specified classes and
	 * {@link Level#WARNING} logging for all classes
	 * @param classes
	 * 		The classes for which to enable finest logging
	 */
	public static void enableConsoleLogging(Class<?>... classes) {
		System.out.println("Enabled console logging...");
		
		//Remove any existing handlers
		Logger rootLogger = Logger.getLogger("");
		Handler[] handlers = rootLogger.getHandlers();
		for (Handler handler : handlers) {
			rootLogger.removeHandler(handler);
		}

		//Add our logger
		ConsoleHandler handler = new ConsoleHandler();
		//handler.setFormatter(new SimpleFormatter());
		handler.setLevel(Level.ALL);
		Logger.getLogger("").addHandler(handler);
		Logger.getLogger("").setLevel(Level.WARNING);
		for (Class<?> c : classes) {
			Logger.getLogger(c.getCanonicalName()).setLevel(Level.FINEST);
		}
	}

	/**
	 * Get a {@link Logger} for a class
	 * @param clazz
	 * 		The class
	 * @return
	 * 		The logger
	 */
	public static Logger logger(Class<?> clazz) {
		return Logger.getLogger(clazz.getCanonicalName());
	}
}
